package pl.edu.agh.ki.neuralnetwork.builder;

import java.io.FileReader;

import pl.edu.agh.ki.neuralnetwork.builder.xml.XmlConfigurationException;
import pl.edu.agh.ki.neuralnetwork.builder.xml.XmlNetworkParser;
import pl.edu.agh.ki.neuralnetwork.network.NeuralNetwork;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

/**
 * Neural network builder that reads network structure from xml file.
 * @author ravn
 *
 */
public class XmlNetworkBuilder implements NeuralNetworkBuilder {
	private XMLReader reader;
	private XmlNetworkParser handler;
	private String file;
	
	/**
	 * 
	 * @param path Xml file's path
	 */
    public XmlNetworkBuilder(String path) {
    	this.reader = new SAXParser();
    	this.handler = new XmlNetworkParser();
    	this.file = path;
    	reader.setContentHandler(handler);
    }

    public NeuralNetwork build() throws XmlConfigurationException {
    	FileReader r;
		try {
			r = new FileReader(this.file);
		    reader.parse(new InputSource(r));
		} catch (Exception e) {
			throw new XmlConfigurationException();
		}
		return handler.getNetwork();
    }

}